﻿///// Buttons2 /////

using System;
using System.Collections.Generic;
using System.Collections;
//using System.Linq;
using System.Text;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace Buttons2
{
    class Program
    {
        static void Main(string[] args)
        {
            // Demonstrates the different types of buttons.
            // 
            // @param args:  no arguments needed here

            Console.WriteLine("Chapter 15: example Buttons2");
            Console.WriteLine("-> Creates a PDF file with widget annotations of type button;");
            Console.WriteLine("-> files generated:");
            Console.WriteLine("   buttons2.pdf");

            // step 1: creation of a document-object
            Document document = new Document();
            try
            {
                // step 2:
                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("buttons2.pdf", FileMode.Create));
                // step 3:
                document.Open();
                writer.AddJavaScript("function showButtonState() {\n"
                        + "app.alert('Checkboxes:"
                        + " English: ' + this.getField('English').value + "
                        + "' French: ' + this.getField('French').value + "
                        + "' Dutch: ' + this.getField('Dutch').value + "
                        + "' Radioboxes: ' + this.getField('language').value);"
                        + "\n}");
                // step 4:
                PdfContentByte cb = writer.DirectContent;
                BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
                String[] languages = { "English", "French", "Dutch" };
                Rectangle rect;

                PdfFormField language = PdfFormField.CreateRadioButton(writer, true);
                language.FieldName = "language";
                language.ValueAsName = languages[0];

                for (int i = 0; i < languages.Length; i++)
                {
                    rect = new Rectangle(40, 806 - i * 40, 60, 788 - i * 40);
                    addRadioButton(writer, rect, language, languages[i], i == 0);
                    cb.BeginText();
                    cb.SetFontAndSize(bf, 18);
                    cb.ShowTextAligned(Element.ALIGN_LEFT, languages[i], 70, 790 - i * 40, 0);
                    cb.EndText();
                }

                writer.AddAnnotation(language);

                for (int i = 0; i < languages.Length; i++)
                {
                    rect = new Rectangle(260, 806 - i * 40, 280, 788 - i * 40);
                    createCheckbox(writer, rect, languages[i]);
                    cb.BeginText();
                    cb.SetFontAndSize(bf, 18);
                    cb.ShowTextAligned(Element.ALIGN_LEFT, languages[i], 290, 790 - i * 40, 0);
                    cb.EndText();
                }

                PdfTemplate template = cb.CreateTemplate(150, 150);
                template.SetLineWidth(12f);
                template.Arc(40f - (float)Math.Sqrt(12800), 110f + (float)Math.Sqrt(12800), 200f - (float)Math.Sqrt(12800), -50f + (float)Math.Sqrt(12800), 281.25f, 33.75f);
                template.Arc(40f, 110f, 200f, -50f, 90f, 45f);
                template.Stroke();
                template.SetLineCap(PdfContentByte.LINE_JOIN_ROUND);
                template.Arc(80f, 30f, 160f, 110f, 90f, 180f);
                template.Arc(115f, 65f, 125f, 75f, 0f, 360f);
                template.Stroke();

                PushbuttonField push = new PushbuttonField(writer, new Rectangle(40, 650, 150, 680), "pushAction");
                push.BackgroundColor = BaseColor.YELLOW;
                push.BorderColor = BaseColor.BLACK;
                push.Text = "Push";
                push.TextColor = BaseColor.RED;
                push.Template = template;
                push.ScaleIcon = PushbuttonField.SCALE_ICON_ALWAYS;
                push.Layout = PushbuttonField.LAYOUT_ICON_LEFT_LABEL_RIGHT;
                PdfFormField pushbutton = push.Field;
                pushbutton.Action = PdfAction.JavaScript("this.ShowButtonState()", writer);
                writer.AddAnnotation(pushbutton);
            }

            catch (Exception e)
            {
                Console.Error.WriteLine(e.Message);
            }

            // step 5: we close the document
            document.Close();

            Console.ReadLine();
        }

        private static void addRadioButton(PdfWriter writer, Rectangle rect, PdfFormField radio, String name, bool on)
        {
            RadioCheckField check = new RadioCheckField(writer, rect, null, name);
            check.CheckType = RadioCheckField.TYPE_STAR;
            check.Checked = on;
            radio.AddKid(check.RadioField);
        }

        private static void createCheckbox(PdfWriter writer, Rectangle rect, String name)
        {
            RadioCheckField check = new RadioCheckField(writer, rect, name, "On");
            check.CheckType = RadioCheckField.TYPE_CROSS;
            writer.AddAnnotation(check.CheckField);
        }
    }
}
